package com.example.xsssglxt.controller;

import cn.afterturn.easypoi.entity.vo.NormalExcelConstants;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.view.PoiBaseView;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.alibaba.excel.EasyExcel;
import com.example.xsssglxt.entity.Student;
import com.example.xsssglxt.mapper.StudentMapper;
import com.example.xsssglxt.service.StudentIService;
import com.example.xsssglxt.service.StudentService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.transform.Result;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Controller
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private StudentIService studentIService;
@PostMapping("/add")
public Map<String,Object> save(@RequestBody Student student){
    Map<String,Object> res=new HashMap<>();
    if(studentIService.student(student)==1){
        res.put("code",200);
        res.put("msg","操作成功");
    }
    return res;
}
    @PostMapping("/xgxs")
    public Map<String,Object> updatexs(@RequestBody Student student){
        Map<String,Object> res=new HashMap<>();
        System.out.println(studentIService.change(student));
        if(studentIService.change(student) == 1){
            res.put("code",200);
            res.put("msg","修改成功");
        }
        return res;
    }
    @DeleteMapping("/{id}")
    public  Map<String,Object> delete(@PathVariable Integer id){
        Integer col=studentMapper.deletxs(id);
        Map<String,Object> res=new HashMap<>();

        res.put("data", col);
        return res;
    }
@GetMapping("/index")
    public Map<String,Object> index(@RequestParam Integer pageNum, @RequestParam Integer pageSize , @RequestParam String name, @RequestParam String sn) {
        pageNum=(pageNum-1)*pageSize;

        List<Student> data= studentMapper.selectPage(pageNum,pageSize,name,sn);
        Integer total = studentMapper.selectTotal(name,sn);
    Map<String, Object> res = new HashMap<>();
        res.put("data",data);
        res.put("total",total);


        return  res;
    }
    @GetMapping("/indexx")
    public Map<String,Object> indexx() {


        List<Student> data= studentMapper.selectlist();

        Map<String, Object> res = new HashMap<>();
        res.put("data",data);


        return  res;
    }

    @GetMapping("/export")
    @ApiOperation(value = "导出会员列表Excel")

    public void exportMemberList(ModelMap map,
                                 HttpServletRequest request,
                                 HttpServletResponse response) {

        List<Student> memberList = LocalJsonUtil.getListFromJson("/student.json", Student.class);
        ExportParams params = new ExportParams("会员列表", "会员列表", ExcelType.XSSF);
        map.put(NormalExcelConstants.DATA_LIST, memberList);
        map.put(NormalExcelConstants.CLASS, Student.class);
        map.put(NormalExcelConstants.PARAMS, params);
        map.put(NormalExcelConstants.FILE_NAME, "memberList");
        PoiBaseView.render(map, request, response, NormalExcelConstants.EASYPOI_EXCEL_VIEW);
    }
    @GetMapping("/down")
    public Result down(HttpServletResponse response) throws IOException {
List<Student> studentList=studentMapper.selectlist();
response.setContentType("application/octet-stream");
String fileName=URLEncoder.encode("学生表","UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(),Student.class).autoCloseStream(Boolean.FALSE).sheet("学生表").doWrite(studentList);


        return null;
    }
    @GetMapping("/indown")
    public Result upload(@RequestParam("files") MultipartFile file) throws IOException{
    EasyExcel.read(file.getInputStream(),Student.class,new DataListener(studentMapper)).sheet().doRead();
    return null;

    }

/**
 *
 * @param file
 * @throws Exception
 **/
@PostMapping("/import")
    public Boolean imp(MultipartFile file) throws Exception{
    InputStream inputStream=file.getInputStream();
//   ExcelReader reader=ExcelUtil.getReader(inputStream);
    ExcelReader reader= ExcelUtil.getReader(inputStream);
List<List<Object>> list=reader.read(1);
List<Student> students= CollUtil.newArrayList();
for(List<Object> row:list){
    Student student=new Student();
    student.setId(row.get(0).toString());
    student.setName(row.get(1).toString());
    student.setPassword(row.get(2).toString());
    student.setSn(row.get(3).toString());

students.add(student);
}


    return true;
}

    }
